package org.springboot.szsrcb.controller;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.sql.Blob;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springboot.szsrcb.srvice.Excel_Service;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dhtmlx.xml2excel.ExcelWriter;

@Controller
@SuppressWarnings("serial")
public class ExcelGenerator extends HttpServlet {

	@Autowired
	private Excel_Service excel_Service;

	@RequestMapping("/ExcelGenerator")
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String xml = req.getParameter("grid_xml");
		xml = URLDecoder.decode(xml, "UTF-8");
		(new ExcelWriter()).generate(xml, resp);
	}
	
	@RequestMapping("/ExcelGenerator_req_projec_trace")
	public void ExcelGenerator_req_projec_trace(HttpServletRequest request, HttpServletResponse resp) throws IOException {
		//String get_test_id = (String) request.getParameter("test_id");
		InputStream model_is = new FileInputStream("d:\\SZSRCB\\req_project_trace.xls");
		// ExcelReader excelReader = new ExcelReader();
		InputStream o_is=excel_Service.req_projec_trace(model_is);
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
		Date d=new Date();		
		resp.setHeader("Content-Disposition", "attachment; filename=req_project_trace"+sdf2.format(d)+".xls" );
		resp.setContentType("application/x-msdownload;charset=utf-8");
		
		try {
			// response.flushBuffer();
			//Blob blob = att.getAtt_data();
			// String filetype=(String) list.get(1);
			//int length = (int) blob.length();
			
			InputStream is = o_is;
			byte[] buffer = new byte[is.available()];
			is.read(buffer);
			OutputStream out = resp.getOutputStream();
			out.write(buffer);
			out.flush();
			out.close();
			is.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}
	
	@RequestMapping("/ExcelGenerator_test_projec_trace")
	public void ExcelGenerator_test_projec_trace(HttpServletRequest request, HttpServletResponse resp) throws IOException {
		//String get_test_id = (String) request.getParameter("test_id");
		InputStream model_is = new FileInputStream("d:\\SZSRCB\\test_project_trace.xls");
		// ExcelReader excelReader = new ExcelReader();
		InputStream o_is=excel_Service.test_projec_trace(model_is);
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
		Date d=new Date();		
		resp.setHeader("Content-Disposition", "attachment; filename=test_project_trace"+sdf2.format(d)+".xls" );
		resp.setContentType("application/x-msdownload;charset=utf-8");
		
		try {
			// response.flushBuffer();
			//Blob blob = att.getAtt_data();
			// String filetype=(String) list.get(1);
			//int length = (int) blob.length();
			
			InputStream is = o_is;
			byte[] buffer = new byte[is.available()];
			is.read(buffer);
			OutputStream out = resp.getOutputStream();
			out.write(buffer);
			out.flush();
			out.close();
			is.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}

	@RequestMapping("/ExcelGenerator_test_info_sum")
	public void ExcelGenerator_test_info_sum(HttpServletRequest request, HttpServletResponse resp) throws IOException {
		String get_test_id = (String) request.getParameter("test_id");
		InputStream model_is = new FileInputStream("d:\\SZSRCB\\Test_info_sum.xls");
		// ExcelReader excelReader = new ExcelReader();
		InputStream o_is=excel_Service.test_info_sum(model_is,get_test_id);
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
		Date d=new Date();	
		resp.setHeader("Content-Disposition", "attachment; filename=test_info_"+get_test_id+"_"+sdf2.format(d)+".xls" );
		resp.setContentType("application/x-msdownload;charset=utf-8");
		
		try {
			// response.flushBuffer();
			//Blob blob = att.getAtt_data();
			// String filetype=(String) list.get(1);
			//int length = (int) blob.length();
			
			InputStream is = o_is;
			byte[] buffer = new byte[is.available()];
			is.read(buffer);
			OutputStream out = resp.getOutputStream();
			out.write(buffer);
			out.flush();
			out.close();
			is.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}

}